
#include "XmlWriterClass.h"

XmlWriterClass::XmlWriterClass(const char* fileName)
{	
		output.open(fileName);
		output << "<?xml version=\"1.0\"?>"  << endl;
 		output << "<Cena xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"  << endl;
		output << "<Nome>";
		output <<  fileName;
		output << "</Nome>"  << endl;
}


void XmlWriterClass::EndFile(void)
{
	output << "</Cena>"  << endl;
	output.flush();
	output.close();
}

void XmlWriterClass::BeginWriteModels()
{
	output << "<Modelos>" << endl;
}

void XmlWriterClass::EndWriteModels()
{
		output << "</Modelos>"  << endl;
}

void XmlWriterClass::BeginWriteModel(const char* modelName)
{
		output << "<Modelo>" << endl;
		output << "<Name>";
		output << modelName;
	    output << "</Name>" << endl; 
}

void XmlWriterClass::EndWriteModel()
{
		output << "</Modelo>" << endl;
}

void XmlWriterClass::WriteTransformation(Point3 translation,Point4 rotation, Point3 scale)
{
	output << "<Transformation>" << endl;
	output << "<translation>" << endl;
	writePoint(translation);
	output << "</translation>" << endl;
	output << "<rotationQuaternion>" << endl;
	writePoint(rotation);
	output << "</rotationQuaternion>" << endl;
	output << "<Scale>" << endl;
	writePoint(scale);
	output << "</Scale>" << endl;
	output << "</Transformation>" << endl;
        
}

	void XmlWriterClass::BeginWriteVertices(void)
	{
		output << "<Vertices>" << endl;
	}
	void XmlWriterClass::EndWriteVertices(void)
	{
		output << "</Vertices>" << endl;
	}

	void XmlWriterClass::WriteVertice(Point3 position, Point3 normal, Point2 textCoord,Point3 tangent, Point3 biNormal)
	{
		output << "<Vertice>" << endl;
		output << "<Position>" << endl;
		writePoint(position);
		output << "</Position>" << endl;
		output << "<Normal>" << endl;
		writePoint(normal);
		output << "</Normal>" << endl;
		output << "<TextureCoord>" << endl;
		writePoint(textCoord);
		output << "</TextureCoord>" << endl;
		output << "<Tangent>" << endl;
		writePoint(tangent);
		output << "</Tangent>" << endl;
		output << "<BiNormal>" << endl;
		writePoint(biNormal);
		output << "</BiNormal>" << endl;
		output << "</Vertice>" << endl;
		
	}

	
	void XmlWriterClass::BeginWriteIndices(void)
	{
		output << "<Indices>" << endl;
	}
	void XmlWriterClass::EndWriteIndices(void)
	{
		output << "</Indices>" << endl;
	}

	void XmlWriterClass::WriteIndice(int ind)
	{
		output << "<int>";
		output << ind;
		output << "</int>" << endl;
	}

	void XmlWriterClass::BeginWriteMaps(void)
	{
		output << "<Maps>" << endl;
	}
	void XmlWriterClass::EndWriteMaps(void)
	{
		output << "</Maps>" << endl;
	}

	void XmlWriterClass::WriteMap(const char* mapType, const char* mapPath)
	{
		output << "<Map>" << endl;
		output << "<MapType>";
		output << mapType;
		output << "</MapType>" << endl;
		output << "<Path>";
		output << mapPath;
		output << "</Path>" << endl;	
		output << "</Map>" << endl;		
	}

	void XmlWriterClass::BeginWriteSkinInfo(void)
	{
		output << "<SkinInfo>" << endl;		
	}
	void XmlWriterClass::EndWriteSkinInfo(void)
	{
		output << "</SkinInfo>" << endl;		
	}
	void XmlWriterClass::BeginWriteBones(void)
	{
		output << "<Bones>" << endl;		
	}
	void XmlWriterClass::EndWriteBones(void)
	{
		output << "</Bones>" << endl;		
	}

	void XmlWriterClass::WriteBone(const char* parentName, std::vector<const char*> childsName,const char* boneName, std::vector<int> skinIndices,std::vector<float> skinWeights, Point3 translation,Point4 rotation, Point3 scale)
	{
		output << "<Bone>" << endl;
		output << "<BoneName>";
		output << boneName;
		output << "</BoneName>" << endl;

		output << "<BoneParent>";
		output << parentName;
		output << "</BoneParent>" << endl;

		int nC = childsName.size();
		if(nC > 0 )
		{
			output << "<BoneChilds>" << endl;		
			for(int i = 0 ; i < nC ; i++)
			{
				output << "<string>";		
				output << childsName[i];
				output << "</string>" << endl;
			}
			output << "</BoneChilds>" << endl;
		}

		output << "<SkinIndices>" << endl;
		
		int it = skinIndices.size(); 
		for(int i = 0; i < it; i ++)
		{
			output << "<int>";
			output << skinIndices[i];
			output << "</int>" << endl;					
		}

		output << "</SkinIndices>" << endl;

		output << "<SkinWeights>" << endl;
		
		it = skinWeights.size(); 
		for(int i = 0; i < it; i ++)
		{
			output << "<float>";
			output << skinWeights[i];
			output << "</float>" << endl;					
		}
		output << "</SkinWeights>" << endl;

		output << "<BoneMatrix>" << endl;
		output << "<translation>" << endl;
		writePoint(translation);
		output << "</translation>" << endl;
		output << "<rotationQuaternion>" << endl;
		writePoint(rotation);
		output << "</rotationQuaternion>" << endl;
		output << "<Scale>" << endl;
		writePoint(scale);
		output << "</Scale>" << endl;
		output << "</BoneMatrix>" << endl;	
		output << "</Bone>" << endl;

	}

	void XmlWriterClass::BeginWriteAnimationInfos(const char*  animationName)
	{
		output << "<AnimationInfos>" << endl;
		output << "<AnimationInfo>" << endl;
		output << "<AnimationName>" ;
		output << animationName;
		output << "</AnimationName>" << endl;
		output << "<AnimationClips>" << endl;
		     
	}
	void XmlWriterClass::EndWriteAnimationInfos()
	{
		output << "</AnimationClips>" << endl;
		output << "</AnimationInfo>" << endl;
		output << "</AnimationInfos>" << endl;
	}
	void XmlWriterClass::WriteAnimationClip(const char* boneName , float time,Point3 translation,Point4 rotation, Point3 scale)
	{
			output << "<AnimationClip>" << endl;
			output << "<time>";
			output << time;
			output << "</time>" << endl;
			WriteTransformation(translation,rotation,scale);
			output << "<BoneName>";
			output << boneName;
			output << "</BoneName>" << endl;
			output << "</AnimationClip>" << endl;			
	}
		
	void XmlWriterClass::BeginWriteHelpers(void)
	{
		output << "<Helpers>" << endl;					
	}
	void XmlWriterClass::EndWriteHelpers(void)
	{		
		output << "</Helpers>" << endl;			
	}
	void XmlWriterClass::WriteHelper(const char* Name,Point3 translation,Point4 rotation, Point3 scale )
	{
		output << "<Helper>" << endl;			
		output << "<Name>" ;			
		output << Name;
		output << "</Name>" << endl;			
		WriteTransformation(translation,rotation,scale);
		output << "</Helper>" << endl;			
	}  


	void XmlWriterClass::BeginWriteCameras(void)
	{
		output << "<Cameras>" << endl;					
	}
	void XmlWriterClass::EndWriteCameras(void)
	{
		output << "</Cameras>" << endl;					
	}
	void XmlWriterClass::WriteCamera(const char* nome, float nearPlane, float farPlane, float fov, Point3 Position, Point3 Target)
	{
		output << "<Camera>" << endl;
		output << "<Name>" ;					
		output << nome;
		output << "</Name>" << endl;			
		output << "<NearPlane>" ;					
		output << nearPlane;
		output << "</NearPlane>" << endl;					
		output << "<FarPlane>" ;					
		output << farPlane;
		output << "</FarPlane>" << endl;					
		output << "<Fov>" ;					
		output << fov;
		output << "</Fov>" << endl;		
		output << "<Position>" << endl;					
		writePoint(Position);
		output << "</Position>" << endl;		
		output << "<Target>" << endl;					
		writePoint(Target);
		output << "</Target>" << endl;		
		output << "</Camera>" << endl;					
}   


void XmlWriterClass::writePoint(Point3 pt)
{
	output << "<x>" << pt.x << "</x>" << endl;
	output << "<y>" << pt.y << "</y>" << endl;
	output << "<z>" << pt.z << "</z>" << endl;
}

void XmlWriterClass::writePoint(Point2 pt)
{
	output << "<x>" << pt.x << "</x>" << endl;
	output << "<y>" << pt.y << "</y>" << endl;	
}

void XmlWriterClass::writePoint(Point4 pt)
{
	output << "<x>" << pt.x << "</x>" << endl;
	output << "<y>" << pt.y << "</y>" << endl;
	output << "<z>" << pt.z << "</z>" << endl;
	output << "<w>" << pt.w << "</w>" << endl;
}


XmlWriterClass::~XmlWriterClass(void)
{
}
